package com.dianping.cat.consumer.problem.model.transform;

import com.dianping.cat.consumer.problem.model.IEntity;
import com.dianping.cat.consumer.problem.model.IVisitor;
import com.dianping.cat.consumer.problem.model.entity.Duration;
import com.dianping.cat.consumer.problem.model.entity.Entity;
import com.dianping.cat.consumer.problem.model.entity.Entry;
import com.dianping.cat.consumer.problem.model.entity.GraphTrend;
import com.dianping.cat.consumer.problem.model.entity.JavaThread;
import com.dianping.cat.consumer.problem.model.entity.Machine;
import com.dianping.cat.consumer.problem.model.entity.ProblemReport;
import com.dianping.cat.consumer.problem.model.entity.Segment;
import java.util.Stack;

public class DefaultMerger
  implements IVisitor
{
/*  18 */   private Stack<Object> m_objs = new Stack();

  private ProblemReport m_problemReport;


  public DefaultMerger() {}

  public DefaultMerger(ProblemReport problemReport) {
/*  26 */     this.m_problemReport = problemReport;
/*  27 */     this.m_objs.push(problemReport);
  }


  public ProblemReport getProblemReport() { return this.m_problemReport; }



/*  35 */   protected Stack<Object> getObjects() { return this.m_objs; }


  public <T> void merge(IEntity<T> to, IEntity<T> from) {
/*  39 */     this.m_objs.push(to);
/*  40 */     from.accept(this);
/*  41 */     this.m_objs.pop();
  }

  protected void mergeDuration(Duration to, Duration from) {
/*  45 */     to.mergeAttributes(from);
/*  46 */     to.getMessages().addAll(from.getMessages());
  }


/*  50 */   protected void mergeEntity(Entity to, Entity from) { to.mergeAttributes(from); }



/*  54 */   protected void mergeEntry(Entry to, Entry from) { to.mergeAttributes(from); }



/*  58 */   protected void mergeGraphTrend(GraphTrend to, GraphTrend from) { to.mergeAttributes(from); }



/*  62 */   protected void mergeMachine(Machine to, Machine from) { to.mergeAttributes(from); }


  protected void mergeProblemReport(ProblemReport to, ProblemReport from) {
/*  66 */     to.mergeAttributes(from);
/*  67 */     to.getDomainNames().addAll(from.getDomainNames());
/*  68 */     to.getIps().addAll(from.getIps());
  }

  protected void mergeSegment(Segment to, Segment from) {
/*  72 */     to.mergeAttributes(from);
/*  73 */     to.getMessages().addAll(from.getMessages());
  }


/*  77 */   protected void mergeThread(JavaThread to, JavaThread from) { to.mergeAttributes(from); }



  public void visitDuration(Duration from) {
/*  82 */     Duration to = (Duration)this.m_objs.peek();

/*  84 */     mergeDuration(to, from);
/*  85 */     visitDurationChildren(to, from);
  }


  protected void visitDurationChildren(Duration to, Duration from) {}


  public void visitEntity(Entity from) {
/*  93 */     Entity to = (Entity)this.m_objs.peek();

/*  95 */     mergeEntity(to, from);
/*  96 */     visitEntityChildren(to, from);
  }

  protected void visitEntityChildren(Entity to, Entity from) {
/* 100 */     for (JavaThread source : from.getThreads().values()) {
/* 101 */       JavaThread target = to.findThread(source.getId());

/* 103 */       if (target == null) {
/* 104 */         target = new JavaThread(source.getId());
/* 105 */         to.addThread(target);
      }

/* 108 */       this.m_objs.push(target);
/* 109 */       source.accept(this);
/* 110 */       this.m_objs.pop();
    }

/* 113 */     for (Duration source : from.getDurations().values()) {
/* 114 */       Duration target = to.findDuration(source.getValue());

/* 116 */       if (target == null) {
/* 117 */         target = new Duration(source.getValue());
/* 118 */         to.addDuration(target);
      }

/* 121 */       this.m_objs.push(target);
/* 122 */       source.accept(this);
/* 123 */       this.m_objs.pop();
    }

/* 126 */     if (from.getGraphTrend() != null) {
/* 127 */       GraphTrend target = to.getGraphTrend();

/* 129 */       if (target == null) {
/* 130 */         GraphTrend source = from.getGraphTrend();

/* 132 */         target = new GraphTrend(source.getDuration());
/* 133 */         to.setGraphTrend(target);
      }

/* 136 */       this.m_objs.push(target);
/* 137 */       from.getGraphTrend().accept(this);
/* 138 */       this.m_objs.pop();
    }
  }


  public void visitEntry(Entry from) {
/* 144 */     Entry to = (Entry)this.m_objs.peek();

/* 146 */     mergeEntry(to, from);
/* 147 */     visitEntryChildren(to, from);
  }

  protected void visitEntryChildren(Entry to, Entry from) {
/* 151 */     for (Duration source : from.getDurations().values()) {
/* 152 */       Duration target = to.findDuration(source.getValue());

/* 154 */       if (target == null) {
/* 155 */         target = new Duration(source.getValue());
/* 156 */         to.addDuration(target);
      }

/* 159 */       this.m_objs.push(target);
/* 160 */       source.accept(this);
/* 161 */       this.m_objs.pop();
    }

/* 164 */     for (JavaThread source : from.getThreads().values()) {
/* 165 */       JavaThread target = to.findThread(source.getId());

/* 167 */       if (target == null) {
/* 168 */         target = new JavaThread(source.getId());
/* 169 */         to.addThread(target);
      }

/* 172 */       this.m_objs.push(target);
/* 173 */       source.accept(this);
/* 174 */       this.m_objs.pop();
    }
  }


  public void visitGraphTrend(GraphTrend from) {
/* 180 */     GraphTrend to = (GraphTrend)this.m_objs.peek();

/* 182 */     mergeGraphTrend(to, from);
/* 183 */     visitGraphTrendChildren(to, from);
  }


  protected void visitGraphTrendChildren(GraphTrend to, GraphTrend from) {}


  public void visitMachine(Machine from) {
/* 191 */     Machine to = (Machine)this.m_objs.peek();

/* 193 */     mergeMachine(to, from);
/* 194 */     visitMachineChildren(to, from);
  }

  protected void visitMachineChildren(Machine to, Machine from) {
/* 198 */     for (Entry source : from.getEntries()) {
/* 199 */       Entry target = null;

/* 201 */       if (target == null) {
/* 202 */         target = new Entry();
/* 203 */         to.addEntry(target);
      }

/* 206 */       this.m_objs.push(target);
/* 207 */       source.accept(this);
/* 208 */       this.m_objs.pop();
    }

/* 211 */     for (Entity source : from.getEntities().values()) {
/* 212 */       Entity target = to.findEntity(source.getId());

/* 214 */       if (target == null) {
/* 215 */         target = new Entity(source.getId());
/* 216 */         to.addEntity(target);
      }

/* 219 */       this.m_objs.push(target);
/* 220 */       source.accept(this);
/* 221 */       this.m_objs.pop();
    }
  }


  public void visitProblemReport(ProblemReport from) {
/* 227 */     ProblemReport to = (ProblemReport)this.m_objs.peek();

/* 229 */     mergeProblemReport(to, from);
/* 230 */     visitProblemReportChildren(to, from);
  }

  protected void visitProblemReportChildren(ProblemReport to, ProblemReport from) {
/* 234 */     for (Machine source : from.getMachines().values()) {
/* 235 */       Machine target = to.findMachine(source.getIp());

/* 237 */       if (target == null) {
/* 238 */         target = new Machine(source.getIp());
/* 239 */         to.addMachine(target);
      }

/* 242 */       this.m_objs.push(target);
/* 243 */       source.accept(this);
/* 244 */       this.m_objs.pop();
    }
  }


  public void visitSegment(Segment from) {
/* 250 */     Segment to = (Segment)this.m_objs.peek();

/* 252 */     mergeSegment(to, from);
/* 253 */     visitSegmentChildren(to, from);
  }


  protected void visitSegmentChildren(Segment to, Segment from) {}


  public void visitThread(JavaThread from) {
/* 261 */     JavaThread to = (JavaThread)this.m_objs.peek();

/* 263 */     mergeThread(to, from);
/* 264 */     visitThreadChildren(to, from);
  }

  protected void visitThreadChildren(JavaThread to, JavaThread from) {
/* 268 */     for (Segment source : from.getSegments().values()) {
/* 269 */       Segment target = to.findSegment(source.getId());

/* 271 */       if (target == null) {
/* 272 */         target = new Segment(source.getId());
/* 273 */         to.addSegment(target);
      }

/* 276 */       this.m_objs.push(target);
/* 277 */       source.accept(this);
/* 278 */       this.m_objs.pop();
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/problem/model/transform/DefaultMerger.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */